﻿@{
    ViewBag.Title = "模块按钮";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">模块结构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;"><i class="fa fa-minus"></i></a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">模块按钮列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default"><i class="fa fa-search"></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
@Html.Partial("_PartialSystemPrivilegeDetail")
<script>
    $(function () {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initGird();
        initTreeData();
    });

    var zNodes,
        treeObj,
        $grid,
        height,
        template;

    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            shrinkToFit: true,
            multiselect: false,
            url: "/System/MenuButton/GetMenuButtonByMenuId",
            colModel: [
                        { name: "MenuButtonId", hidden: true },
                        { label: "图标", align: "center", name: "Icon", width: 50, fixed: true, formatter: "icon", search: false },
                        { label: "名称", name: "Name", width: 150, fixed: true },
                        { label: "归属菜单", name: "MenuName", width: 150, fixed: true },
                        { label: "事件", name: "Script", width: 50 },
                        { label: "排序", name: "OrderNo", width: 50, align: "center", fixed: true },
                        //{ label: "分割线", name: "Divider", width: 50, align: "center", formatter: "YesOrNo", fixed: true },
                        { label: "备注", name: "Remark", width: 50 }
            ],
            height: height - 100,
            postData: { pId: Language.common.guidempty },
            rowList: [200, 500, 1000],
            subGrid: true,
            subGridRowExpanded: function (subgridId, rowId) {
                var rowDatas = $grid.jqGrid('getRowData', rowId);
                if (typeof (rowDatas["MenuButtonId"]) != "undefined") {
                    getById(Language.privilegeAccess.func, rowDatas["MenuButtonId"]);
                    //获取html值
                    $("#" + subgridId).html($("#permissionUser").html());
                }
            }
        });
    }

    //获取详细信息
    function getById(access, id) {
        if (id != null && id != "") {
            UtilAjaxPost("/Common/Global/GetSystemPrivilegeDetailOutputsByAccessAndValue", { id: id, access: access },
                function (val) {
                    //角色
                    var roleHtml = template('role-template', val);
                    $("#role").html(roleHtml);

                    //岗位
                    var postHtml = template('post-template', val);
                    $("#post").html(postHtml);

                    //组
                    var groupHtml = template('group-template', val);
                    $("#group").html(groupHtml);

                    //组织机构
                    var organizationHtml = template('organization-template', val);
                    $("#organization").html(organizationHtml);

                    //人员
                    var userHtml = template('user-template', val);
                    $("#user").html(userHtml);
                }
            );
        } else {

            var val = [];
            //角色
            var roleHtml = template('role-template', val);
            $("#role").html(roleHtml);

            //岗位
            var postHtml = template('post-template', val);
            $("#post").html(postHtml);

            //组
            var groupHtml = template('group-template', val);
            $("#group").html(groupHtml);

            //组织机构
            var organizationHtml = template('organization-template', val);
            $("#organization").html(organizationHtml);

            //人员
            var userHtml = template('user-template', val);
            $("#user").html(userHtml);
        }
    }

    //获取表格数据
    function getGridData() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var pId;
        if (treeNode.length === 0) {
            pId = Language.common.guidempty;
        } else {
            pId = treeNode[0].id;
        }
        UtilAjaxPost("/System/MenuButton/GetMenuButtonByMenuId", { id: pId }, function (data) {
            GridReloadLoadOnceData($grid, data);
        });
    }

    //初始化菜单
    function initTree() {
        //配置
        var setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClickTree
            }
        };
        treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    }

    //初始化树结构
    function initTreeData() {
        UtilAjaxPostAsync("/System/Menu/GetHaveMenuButtonPermissionMenu", null, function (data) {
            zNodes = data;
            initTree();
        });
    }

    //树点击触发
    function onClickTree(e, treeId, treeNode) {
        //加载数据
        getGridData();
    }

    //加载树和列表数据
    function reload() {
        initTreeData();
        getGridData();
    }

    //操作:新增
    function add() {
        var pid = Language.common.guidempty;
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        if (treeNode.length !== 0) {
            pid = treeNode[0].id;
        }
        ArtDialogOpen("/System/MenuButton/Edit?menuId=" + pid, "新增菜单按钮", 590);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/MenuButton/Edit?menubuttonId=" + info.MenuButtonId, "修改菜单按钮-" + info.Name, 590);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function () {
            ArtDialogConfirm("<lable style='color:red;font-size:14px'>删除前请确认该菜单按钮权限使用者已取消该菜单按钮?</lable><br/>删除后不可恢复,确定删除?", function () {
                UtilAjaxPostWait(
                     "/System/MenuButton/DeleteMenuButton",
                     { id: GridGetSingSelectData($grid).MenuButtonId },
                     perateStatus
                 );
            });
        });
    }

    //设置关联方法
    function association() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/MenuButton/HaveFunctions?menubuttonId=" + info.MenuButtonId + "&title=【" + info.MenuName + "-" + info.Name + "】", "查看关联模块按钮-【" + info.MenuName + "-" + info.Name + "】", true, 410, 790);
        });
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            //获取重新加载前选中树
            var breforeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
            //重新获取该选中树
            if (breforeNode.length !== 0) {
                //获取选中前节点
                var note = treeObj.getNodeByParam("id", breforeNode[0].id, null);
                //获取选中前节点
                treeObj.selectNode(note);
                treeObj.expandNode(note);
            }
            getGridData();
        }
    }

    var menuObj;
    //请求完成后使用
    function loadTreeAndGrid() {
       var note = treeObj.getNodeByParam("id", menuObj.id, null);
        //选中加载前节点
        treeObj.selectNode(note);
        treeObj.expandNode(note);
        //加载列表数据
        getGridData();
    }
</script>